/* $RCSFile: WSListDataType.cs $
 * Apache license v2 2011 by Christian Surieux
 * $Source: ?/ADNCRM.Core/UI/DataTypes/WSListDataType.cs $
 * $log$
 * Revision 6 2010/08/06 19:56:29  christian.surieux
 *   Added Log to All cs files
 */

using System;
using System.Diagnostics;
using System.IO;
using System.ComponentModel;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Globalization;

using AddonNice.Diagnostics;
using AddonNice.UI;
using AddonNice.UI.WebControls;
using AddonNice.Configuration;

using AddonNice.CRMWrapper;

using Telerik.Web.UI;

namespace AddonNice.UI.DataTypes
{


    public class WSListEventArgs : EventArgs
    {
        public string entityName;
        public string Val;

        public WSListEventArgs(string entName,string val)
        {
            entityName  =   entName;
            Val         =   val;
        }
    }

    public delegate void NeedDisplayTextEventHandler(BaseDataType sender,WSListEventArgs ev);

    /// <summary>
	/// WSListDataType: RadCombobow powered by WSRadCombo
	/// </summary>
	public class WSListDataType :  BaseDataType
	{
		protected string _entityName            =   CrmStr.account; //default
		protected string _dataValueField        =   string.Empty;
		protected string _dataTextField         =   string.Empty;
        
        public event NeedDisplayTextEventHandler NeedDisplayTextEvent;

        protected string _DisplayTextValue      =   string.Empty;
        
        const string strWebServiceMethod        =   "WebServiceMethod";
        
        string _WebServiceMethod                =   string.Empty;
        public string WebServiceMethod
        {
            get
            {
                return _WebServiceMethod;
            }
            set
            {
                _WebServiceMethod               =   value;
            }
        }

        string _WebServicePath                  =   "/Services/CrmEntities.asmx";
        public string WebServicePath
        {
            get
            {
                return _WebServicePath;
            }
            set
            {
                _WebServicePath                 =   value;
            }
        }

        protected RadComboBoxSort _sortOrder    =    RadComboBoxSort.None;
        public RadComboBoxSort SortOrder
        {
            get
            {
                return _sortOrder;
            }
            set
            {
                _sortOrder                      =   value;
            }
        }

		public override bool HasCrmEntity
		{
			get
			{
				return !string.IsNullOrEmpty(_entityName);
			}
		}

		public override string CrmEntity
		{
			get
			{
				return _entityName;
			}
            set
            {
                _entityName  =   value;
            }
		}
        
        public string DisplayTextValue
		{
			get
			{
				return _DisplayTextValue;
			}
			set
			{
				_DisplayTextValue               =   value;
			}
		}

        public override string DataValueField
		{
			get
			{
				return _dataValueField;
			}
			set
			{
				_dataValueField                 =   value;
			}
		}

		public override string DataTextField
		{
			get
			{
				return _dataTextField;
			}
			set
			{
				_dataTextField = value;
			}
		}

        public WSListDataType(string entName,string dataTextField, string dataValueField) : base()
		{
			InnerDataType	        =   PropertiesDataType.List;
			DataValueField	        =   dataValueField;
			DataTextField	        =   dataTextField;
            _entityName             =   entName;
            if ( entName == CrmStr.systemuser )
                WebServiceMethod    =   "GetSyStemUsers";
            else if ( entName == CrmStr.account )
                WebServiceMethod    =   "GetAccounts";
            else if ( entName == CrmStr.contact )
                WebServiceMethod    =   "GetContacts";
            else if ( entName == CrmStr.lead )
                WebServiceMethod    =   "GetLeads";
            else if ( entName == CrmStr.product )
                WebServiceMethod    =   "GetProducts";
            else if ( entName == CrmStr.salesorder )
                WebServiceMethod    =   "GetOrders";
            else if ( entName == CrmStr.quote )
                WebServiceMethod    =   "GetQuotes";
            else if ( entName == CrmStr.invoice )
                WebServiceMethod    =   "GetInvoices";
            else if ( entName == CrmStr.opportunity )
                WebServiceMethod    =   "GetOpportunities";
            else if ( entName == CrmStr.activity )
                WebServiceMethod    =   "GetActivities";
            else if ( entName == CrmStr.incident )
                WebServiceMethod    =   "GetIncidents";
            else if ( entName == CrmStr.task )
                WebServiceMethod    =   "GetTasks";
            else if ( entName == CrmStr.phonecall )
                WebServiceMethod    =   "GetPhoneCalls";
            else if ( entName == CrmStr.email )
                WebServiceMethod    =   "GetEMails";
            else if ( entName == CrmStr.letter )
                WebServiceMethod    =   "GetLetters";
            else if ( entName == CrmStr.appointment )
                WebServiceMethod    =   "GetAppointments";
            else if ( entName == CrmStr.discounttype )
                WebServiceMethod    =   "GetDiscountTypes";
            else if ( entName == CrmStr.list )
                WebServiceMethod    =   "GetLists";
            else if ( entName == CrmStr.kbarticle )
                WebServiceMethod    =   "GetKbArticles";
            else if ( entName == CrmStr.contract )
                WebServiceMethod    =   "GetContracts";
            else if ( entName == CrmStr.pricelevel )
                WebServiceMethod    =   "GetPriceLevels";
            else if ( entName == CrmStr.territory )
                WebServiceMethod    =   "GetTerritories";
            else if ( entName == CrmStr.transactioncurrency )
                WebServiceMethod    =   "GetCurrencies";
            else if ( entName == CrmStr.uom )
                WebServiceMethod    =   "GetUnits";
            else if ( entName == CrmStr.uomschedule )
                WebServiceMethod    =   "GetUnitFamilies";
            else if ( entName == CrmStr.annotation )
                WebServiceMethod    =   "GetAnnotations";
            else if ( entName == CrmStr.campaignresponse )
                WebServiceMethod    =   "GetCampaignResponses";
            else if ( entName == CrmStr.campaignactivity )
                WebServiceMethod    =   "GetCampaignActivities";
            else if ( entName == CrmStr.businessunit )
                WebServiceMethod    =   "GetBusinessUnits";
            else if ( entName == CrmStr.campaign )
                WebServiceMethod    =   "GetCampaigns";
            else if ( entName == CrmStr.serviceappointment )
                WebServiceMethod    =   "GetServiceAppointments3";
            else if ( entName == CrmStr.queue )
                WebServiceMethod    =   "GetQueues";
            else if ( entName == CrmStr.savedquery )
                WebServiceMethod    =   "GetSavedQueries";
            else if ( entName == CrmStr.userquery )
                WebServiceMethod    =   "GetUserQueries";
            else if ( entName == "ContactMailTemplate" )
                WebServiceMethod    =   "GetContactMailTemplates";
            else if ( entName == "LeadMailTemplate" )
                WebServiceMethod    =   "GetLeadMailTemplates";
            else if ( entName == "OrderMailTemplate" )
                WebServiceMethod    =   "GetOrderMailTemplates";
            else if ( entName == "AccountMailTemplate" )
                WebServiceMethod    =   "GetAccountMailTemplates";
            else 
            {
                const string msg0   =   "[E]WSListDataType Ctor bad entity name '{0}'";
                string msg          =   string.Format(CultureInfo.InvariantCulture,msg0,entName);
                if ( DataTypeTraceSwitch.Sw.TraceError )
                    Trace.WriteLine( msg,DataTypeTraceSwitch.Sw.Info);
                throw new ArgumentException(msg);
            }
		}

        public WSListDataType(string wwebServiceMethod,string wwebServicePath, string dataTextField, string dataValueField,string entName) : base()
		{
			InnerDataType	    =   PropertiesDataType.List;
            WebServiceMethod    =   wwebServiceMethod;
            WebServicePath      =   wwebServicePath;
			DataValueField	    =   dataValueField;
			DataTextField	    =   dataTextField;
            _entityName         =   entName;
		}

        /// <summary>
        /// For this Ctor entityname default to account
        /// </summary>
        public WSListDataType(string wwebServiceMethod,string wwebServicePath, string dataTextField, string dataValueField) : base()
		{
			InnerDataType	    =   PropertiesDataType.List;
            WebServiceMethod    =   wwebServiceMethod;
            WebServicePath      =   wwebServicePath;
			DataValueField	    =   dataValueField;
			DataTextField	    =   dataTextField;
		}
        
        public void OnNeedDisplayText(WSListEventArgs ev)
        {
		    if ( DataTypeTraceSwitch.Sw.TraceVerbose )
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]WSListDataType NeedDisplayText BEG NeedDisplayTextEvent null: {0}", ( NeedDisplayTextEvent == null )),DataTypeTraceSwitch.Sw.Info);
            if ( NeedDisplayTextEvent != null )
                NeedDisplayTextEvent(this,ev);
            else DefaultResetDisplay(new Guid(ev.Val));
		    if ( DataTypeTraceSwitch.Sw.TraceVerbose )
                Trace.WriteLine( "[V]WSListDataType NeedDisplayText END.",DataTypeTraceSwitch.Sw.Info);
        }

        public void DefaultResetDisplay(Guid custId)
		{
            if ( DataTypeTraceSwitch.Sw.TraceVerbose )
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]CrmInputIncidentSI DefaultResetDisplay BEG Guid: {0}", custId),DataTypeTraceSwitch.Sw.Info);
            if ( custId == Guid.Empty )
                return;
            DynEntity dynac                 =   null;
            switch(_entityName)
            {
                case CrmStr.systemuser:
                dynac                       =   DynEntity.GetFromCache<ADNDynSystemUser>(_entityName,Wrapper,new ResetWrapperHandler(ResetWrapper),custId);
            break;
                case  CrmStr.account:
                dynac                       =   DynEntity.GetFromCache<ADNDynAccount>(_entityName,Wrapper,new ResetWrapperHandler(ResetWrapper),custId);
            break;
                case  CrmStr.contact:
                dynac                       =   DynEntity.GetFromCache<ADNDynContact>(_entityName,Wrapper,new ResetWrapperHandler(ResetWrapper),custId);
            break;
                case  CrmStr.lead:
                dynac                       =   DynEntity.GetFromCache<ADNDynLead>(_entityName,Wrapper,new ResetWrapperHandler(ResetWrapper),custId);
            break;
                case  CrmStr.product:
                dynac                       =   DynEntity.GetFromCache<ADNDynProduct>(_entityName,Wrapper,new ResetWrapperHandler(ResetWrapper),custId);
            break;
                case  CrmStr.salesorder:
                dynac                       =   DynEntity.GetFromCache<ADNDynSalesOrder>(_entityName,Wrapper,new ResetWrapperHandler(ResetWrapper),custId);
            break;
                case  CrmStr.quote:
                dynac                       =   DynEntity.GetFromCache<ADNDynQuote>(_entityName,Wrapper,new ResetWrapperHandler(ResetWrapper),custId);
            break;
                case  CrmStr.invoice:
                dynac                       =   DynEntity.GetFromCache<ADNDynInvoice>(_entityName,Wrapper,new ResetWrapperHandler(ResetWrapper),custId);
            break;
                case  CrmStr.opportunity:
                dynac                       =   DynEntity.GetFromCache<ADNDynOpportunity>(_entityName,Wrapper,new ResetWrapperHandler(ResetWrapper),custId);
            break;
                case  CrmStr.activity:
                dynac                       =   DynEntity.GetFromCache<ADNDynActivity>(_entityName,Wrapper,new ResetWrapperHandler(ResetWrapper),custId);
            break;
                case  CrmStr.incident:
                dynac                       =   DynEntity.GetFromCache<ADNDynIncident>(_entityName,Wrapper,new ResetWrapperHandler(ResetWrapper),custId);
            break;
                case  CrmStr.task:
                dynac                       =   DynEntity.GetFromCache<ADNDynActivityTask>(_entityName,Wrapper,new ResetWrapperHandler(ResetWrapper),custId);
            break;
                case  CrmStr.phonecall:
                dynac                       =   DynEntity.GetFromCache<ADNDynPhoneCall>(_entityName,Wrapper,new ResetWrapperHandler(ResetWrapper),custId);
            break;
                case  CrmStr.email:
                dynac                       =   DynEntity.GetFromCache<ADNDynEmail>(_entityName,Wrapper,new ResetWrapperHandler(ResetWrapper),custId);
            break;
                case  CrmStr.letter:
                dynac                       =   DynEntity.GetFromCache<ADNDynLetter>(_entityName,Wrapper,new ResetWrapperHandler(ResetWrapper),custId);
            break;
                case  CrmStr.appointment:
                dynac                       =   DynEntity.GetFromCache<ADNDynAppointment>(_entityName,Wrapper,new ResetWrapperHandler(ResetWrapper),custId);
            break;
                case  CrmStr.discounttype:
                dynac                       =   DynEntity.GetFromCache<ADNDynDiscountType>(_entityName,Wrapper,new ResetWrapperHandler(ResetWrapper),custId);
            break;
                case  CrmStr.list:
                dynac                       =   DynEntity.GetFromCache<ADNDynList>(_entityName,Wrapper,new ResetWrapperHandler(ResetWrapper),custId);
            break;
                case  CrmStr.kbarticle:
                dynac                       =   DynEntity.GetFromCache<ADNDynKbArticle>(_entityName,Wrapper,new ResetWrapperHandler(ResetWrapper),custId);
            break;
                case  CrmStr.contract:
                dynac                       =   DynEntity.GetFromCache<ADNDynContract>(_entityName,Wrapper,new ResetWrapperHandler(ResetWrapper),custId);
            break;
                case  CrmStr.pricelevel:
                dynac                       =   DynEntity.GetFromCache<ADNDynPricelevel>(_entityName,Wrapper,new ResetWrapperHandler(ResetWrapper),custId);
            break;
                case  CrmStr.territory:
                dynac                       =   DynEntity.GetFromCache<ADNDynTerritory>(_entityName,Wrapper,new ResetWrapperHandler(ResetWrapper),custId);
            break;
                case  CrmStr.transactioncurrency:
                dynac                       =   DynEntity.GetFromCache<ADNDynTransactionCurrency>(_entityName,Wrapper,new ResetWrapperHandler(ResetWrapper),custId);
            break;
                case  CrmStr.uom:
                dynac                       =   DynEntity.GetFromCache<ADNDynUom>(_entityName,Wrapper,new ResetWrapperHandler(ResetWrapper),custId);
            break;
                case  CrmStr.uomschedule:
                dynac                       =   DynEntity.GetFromCache<ADNDynUomSchedule>(_entityName,Wrapper,new ResetWrapperHandler(ResetWrapper),custId);
            break;
                case  CrmStr.annotation:
                dynac                       =   DynEntity.GetFromCache<ADNDynAnnotation>(_entityName,Wrapper,new ResetWrapperHandler(ResetWrapper),custId);
            break;
                case  CrmStr.campaignresponse:
                dynac                       =   DynEntity.GetFromCache<ADNDynCampaignResponse>(_entityName,Wrapper,new ResetWrapperHandler(ResetWrapper),custId);
            break;
                case  CrmStr.campaignactivity:
                dynac                       =   DynEntity.GetFromCache<ADNDynCampaignActivity>(_entityName,Wrapper,new ResetWrapperHandler(ResetWrapper),custId);
            break;
                case  CrmStr.businessunit:
                dynac                       =   DynEntity.GetFromCache<ADNDynBusinessUnit>(_entityName,Wrapper,new ResetWrapperHandler(ResetWrapper),custId);
            break;
                case  CrmStr.campaign:
                dynac                       =   DynEntity.GetFromCache<ADNDynCampaign>(_entityName,Wrapper,new ResetWrapperHandler(ResetWrapper),custId);
            break;
                case  CrmStr.serviceappointment:
                dynac                       =   DynEntity.GetFromCache<ADNDynServiceappointment>(_entityName,Wrapper,new ResetWrapperHandler(ResetWrapper),custId);
            break;
                case  CrmStr.queue:
                dynac                       =   DynEntity.GetFromCache<ADNDynQueue>(_entityName,Wrapper,new ResetWrapperHandler(ResetWrapper),custId);
            break;
                case  CrmStr.savedquery:
                dynac                       =   DynEntity.GetFromCache<ADNDynSavedquery>(_entityName,Wrapper,new ResetWrapperHandler(ResetWrapper),custId);
            break;
                case  CrmStr.userquery:
                dynac                       =   DynEntity.GetFromCache<ADNDynUserquery>(_entityName,Wrapper,new ResetWrapperHandler(ResetWrapper),custId);
            break;
                case  "ContactMailTemplate":
            break;
                case  "LeadMailTemplate":
            break;
                case  "OrderMailTemplate":
            break;
                case  "AccountMailTemplate":
                break;
                default:
                return;
		    }
            DisplayTextValue                =   dynac.PrimaryFieldStringValue;
            if ( DataTypeTraceSwitch.Sw.TraceVerbose )
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]CrmInputIncidentSI DefaultResetDisplay END DisplayTextValue: {0}", DisplayTextValue),DataTypeTraceSwitch.Sw.Info);
        }

        protected override void InitializeComponents()
		{
            if ( DataTypeTraceSwitch.Sw.TraceVerbose )
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]WSListDataType InitializeComponents BEG Creating DropDownList for datatype description: {0}, UseRadControl: {1}", Description,_UsesRadControl),DataTypeTraceSwitch.Sw.Info);
            RadWSCombo dd       =   new RadWSCombo(WebServiceMethod,WebServicePath);
            dd.Width            =   controlWidth;
            dd.Sort             =   SortOrder;
            if (HandleChanges)
            {
                if (DataTypeTraceSwitch.Sw.TraceVerbose)
                    Trace.WriteLine( "[V]WSListDataType InitializeComponents HandleChanges set", DataTypeTraceSwitch.Sw.Info);
                dd.AutoPostBack =   true;
                dd.SelectedIndexChanged +=new RadComboBoxSelectedIndexChangedEventHandler(dd_RadSelectedIndexChanged);
            }
            dd.DataSource       =   DataSource;

            dd.DataValueField   =   DataValueField;
            dd.DataTextField    =   DataTextField;
            if ( ReadOnly )
                dd.Enabled      =   false;
            innerControl        =   dd;
            _IsInitialized      =   true;
		}

        void dd_RadSelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
        {
    		if ( DataTypeTraceSwitch.Sw.TraceVerbose )
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]WSListDataType dd_RadSelectedIndexChanged sender: {0}",sender),DataTypeTraceSwitch.Sw.Info);
            string oldValue         =   Val;
            ReadDataFromEditControl();
            string newValue         =   Val;
            OnDataTypeEvent(new DataTypeEventArgs(DataTypeEventType.Changed,sender,e,oldValue,newValue));
        }
		        
        public override object DataSource
		{
			get
			{
			    if ( DataTypeTraceSwitch.Sw.TraceVerbose )
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]WSListDataType DataSource GET InnerDataSource null: {0}", ( InnerDataSource == null )),DataTypeTraceSwitch.Sw.Info);
				return InnerDataSource;
			}
			set
			{
			    if ( DataTypeTraceSwitch.Sw.TraceVerbose )
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]WSListDataType DataSource SET value null: {0}", ( value == null )),DataTypeTraceSwitch.Sw.Info);
				InnerDataSource = value;
			}
		}

   		public override void ResetDataSource(object newDataSrc,string SelVal)
		{
			if ( DataTypeTraceSwitch.Sw.TraceVerbose )
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]WSListDataType ResetDataSource BEG newDataSrc: {0}, SelVal: {1}", newDataSrc,( SelVal == null )? string.Empty:SelVal),DataTypeTraceSwitch.Sw.Info);
			InnerDataSource =   newDataSrc;
            // Val.set will do a databind
            if ( !string.IsNullOrEmpty(SelVal))
                    Val     =   SelVal;
            else    Val     =   string.Empty;
            if ( HandleChanges )
                OnDataTypeEvent(new DataTypeEventArgs(DataTypeEventType.ReBound,this));
			if ( DataTypeTraceSwitch.Sw.TraceVerbose )
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]WSListDataType ResetDataSource END innerControl null: {0}", innerControl == null ),DataTypeTraceSwitch.Sw.Info);
		}

        public override void DataBind()
        {
			if ( DataTypeTraceSwitch.Sw.TraceVerbose )
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]WSListDataType DataBind BEG for: {0}, UseRadControl: {1}", this,UseRadControl),DataTypeTraceSwitch.Sw.Info);
			try
			{
                RadWSCombo dd       =   (RadWSCombo)innerControl;
                dd.ClearSelection();
                dd.Items.Clear();
                dd.DataValueField   =   DataValueField;
                dd.DataTextField    =   DataTextField;
                dd.DataSource       =   DataSource;
                dd.DataBind();
                if (!string.IsNullOrEmpty(Val))
                {
                    // if somebody has set a handler to install correct display text, call it before building item
                    OnNeedDisplayText(new WSListEventArgs(CrmEntity,Val));
                    RadComboBoxItem it  =   new RadComboBoxItem(DisplayTextValue, Val);
                    dd.Items.Add(it);
                    it.Selected     =   true;
                }
		        if ( DataTypeTraceSwitch.Sw.TraceVerbose )
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]WSListDataType DataBind SelectedValue set to Val: [{0}]", Val),DataTypeTraceSwitch.Sw.Info);
			}
			catch (Exception ex)
			{
                if ( DataTypeTraceSwitch.Sw.TraceError )
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]WSListDataType DataBind ex: {0}",ex),DataTypeTraceSwitch.Sw.Info);
                throw;
			}
			if ( DataTypeTraceSwitch.Sw.TraceVerbose )
                Trace.WriteLine( "[V]WSListDataType DataBind END.",DataTypeTraceSwitch.Sw.Info);
        }

        /// <summary>
        /// Update value from control: used by eventhandler after a control change event triggered
        /// </summary>
        public override string ReadDataFromEditControl()
		{
            Control Ctl =   EditControl;
            if (DataTypeTraceSwitch.Sw.TraceVerbose)
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]WSListDataType ReadDataFromEditControl BEG Control is null: {0}, Val: {1}.", (Ctl == null), Val ), DataTypeTraceSwitch.Sw.Info);
            if ( Ctl ==   null )
                return Val;
            // DO NOT use Val which is doing a databind on set but use InnerValue
            if ( Ctl is RadWSCombo)
            {
                RadWSCombo dd       =   (RadWSCombo)Ctl;
                if (dd.SelectedItem != null)
                    innerValue      =   dd.SelectedItem.Value;
                else innerValue     =   string.Empty;
            }
            if (DataTypeTraceSwitch.Sw.TraceVerbose)
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]WSListDataType ReadDataFromEditControl END InnerValue: {0}.", innerValue), DataTypeTraceSwitch.Sw.Info);
            return Val;
        }

        public override Control EditControl
		{
			get
			{
			    if ( DataTypeTraceSwitch.Sw.TraceVerbose )
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]WSListDataType EditControl GET innerControl is NULL: {0}.",(innerControl == null)),DataTypeTraceSwitch.Sw.Info);
				if (innerControl == null)
					InitializeComponents();
                return innerControl;
			}
			set
			{
                bool nodif        = (innerControl != null) && innerControl.Equals(value);
                if (DataTypeTraceSwitch.Sw.TraceVerbose)
                {
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]WSListDataType EditControl SET innerControl is NULL: {0}", innerControl == null ), DataTypeTraceSwitch.Sw.Info);
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]WSListDataType EditControl SET innerControl equal to value : {0}", nodif), DataTypeTraceSwitch.Sw.Info);
                }
                        
                if (value is RadWSCombo)
                {
                    //Update value from control
                    RadWSCombo dd       =   (RadWSCombo)value;
                    dd.DataValueField   =   DataValueField;
                    dd.DataTextField    =   DataTextField;
                    if (HandleChanges && !dd.AutoPostBack)
                    {
                        dd.AutoPostBack =   true;
                        dd.SelectedIndexChanged  += new RadComboBoxSelectedIndexChangedEventHandler(dd_RadSelectedIndexChanged);
                    }
                    // DO NOT use Value but use InnerValue
                    if ( !string.IsNullOrEmpty(dd.SelectedValue) )
                        innerValue      =   dd.SelectedValue;
                    else innerValue     =   string.Empty;
                    if ( !nodif )
                        innerControl    =   value;
			        if ( DataTypeTraceSwitch.Sw.TraceVerbose )
                        Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]WSListDataType EditControl SET innerValue: '{0}'.",innerValue),DataTypeTraceSwitch.Sw.Info);
                    return;
                }
    			string ErrMsg           =   string.Format("[E]WSListDataType EditControl a RadWSCombo values is required, a '{0} is given",value.GetType().Name);
    		    if ( DataTypeTraceSwitch.Sw.TraceError )
                    Trace.WriteLine(ErrMsg,DataTypeTraceSwitch.Sw.Info);
    			throw new ArgumentException(ErrMsg);
			}
		}


   		public override string Val
		{
			get
			{
				return innerValue;
			}
			set
			{
				if ( DataTypeTraceSwitch.Sw.TraceVerbose )
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]WSListDataType Val SET BEG value: [{0}]", value),DataTypeTraceSwitch.Sw.Info);
				innerValue      =   value;
				if ( DataTypeTraceSwitch.Sw.TraceVerbose )
                    Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]WSListDataType Val SET innerControl is NULL: {0}", innerControl == null ),DataTypeTraceSwitch.Sw.Info);
                if ( innerControl == null )
                    return;
				//update value in control
   				DataBind();
				if ( DataTypeTraceSwitch.Sw.TraceVerbose )
                    Trace.WriteLine("[V]WSListDataType Val END.",DataTypeTraceSwitch.Sw.Info);
			}
		}

	}
}
